//
//  UIView+Extension.swift
//  DoctorEPlus
//
//  Created by 王林波 on 2020/10/27.
//

import Foundation
import UIKit

extension UIView {
    
    func addCorner(conrners: UIRectCorner , radius: CGFloat) {
        self.setNeedsLayout()
        self.layoutIfNeeded()
        let maskPath = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: conrners, cornerRadii: CGSize(width: radius, height: radius))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = self.bounds
        maskLayer.path = maskPath.cgPath
        self.layer.mask = maskLayer
    }
    
    func addLayerCorner(_ cornerRadius: CGFloat, _ borderWidth: CGFloat, _ borderColor: UIColor) {
        self.layer.masksToBounds = cornerRadius != 0 ? true:false
        self.layer.cornerRadius = cornerRadius
        self.layer.borderWidth = borderWidth
        self.layer.borderColor = borderColor.cgColor
    }
    
    
    
    typealias touchBlock = (UIView)->()
    

    private struct toucheActKey{
       static var toucheActKey = "toucheActKey"
    }
    
    @objc dynamic var handleCall: touchBlock! {
            set{
                objc_setAssociatedObject(self,&toucheActKey.toucheActKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
            }
            get{
                if let block = objc_getAssociatedObject(self, &toucheActKey.toucheActKey) as? touchBlock{
                    return block
                }
                return nil
            }
    }
    
    
    @objc dynamic  func addViewAction(action:@escaping  touchBlock) {
        self.handleCall = action
        self.isUserInteractionEnabled = true
        if self.gestureRecognizers?.count != 0 {
            let touchTap = UITapGestureRecognizer.init(target: self, action: #selector(handleTapBlock))
            self.addGestureRecognizer(touchTap)
        }
    }
    
    @objc fileprivate func handleTapBlock() {
        if self.handleCall != nil {
            self.handleCall!(self)
        }
     }
    
    
    
    // 判断是否切过切角
    private struct radiusKey{
       static var key = "radiusKey"
    }
    
    @objc dynamic var haveRaiuds: Bool {
        get {
            let radius = objc_getAssociatedObject(self, &radiusKey.key)
            if radius != nil {
                return radius as! Bool
            }else {
                return false
            }
            
        }
        set {
            objc_setAssociatedObject(self,&radiusKey.key, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_ASSIGN)
        }
    }
    
    
    var bh_x: CGFloat {
        get {
            return self.frame.origin.x
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.x       = newValue
            frame                    = tmpFrame
        }
    }
    
    var bh_y: CGFloat {
        get {
            return self.frame.origin.y
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.y       = newValue
            frame                   = tmpFrame
        }
        
    }
    var bh_width: CGFloat {
        get {
            return self.frame.size.width
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.size.width     = newValue
            frame                   = tmpFrame
        }
    }
    var bh_height: CGFloat {
        get {
            return self.frame.size.height
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.size.height    = newValue
            frame                   = tmpFrame
        }
    }
    var bh_bottom: CGFloat {
        get {
            return self.frame.origin.y+self.frame.size.height
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.y       = newValue - tmpFrame.size.height
            frame                   = tmpFrame
        }
    }
    var bh_right: CGFloat {
        get {
            return self.frame.origin.x+self.frame.size.width
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.x       = newValue - tmpFrame.size.width
            frame                   = tmpFrame
        }
    }
    var bh_centerX: CGFloat {
        get {
            return self.frame.origin.x+(self.frame.size.width/2.0)
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.x       = newValue - tmpFrame.size.width/2.0
            frame                   = tmpFrame
        }
    }
    var bh_centerY: CGFloat {
        get {
            return self.frame.origin.y+(self.frame.size.height/2.0)
        }
        set {
            var tmpFrame : CGRect   = frame
            tmpFrame.origin.y       = newValue - tmpFrame.size.height/2.0
            frame                   = tmpFrame
        }
    }
    
    
    
    // 渐变色
    func addGradienLayer(_ colors: [UIColor],_ locations: [NSNumber],_ startPoint: CGPoint?, _ endPoint: CGPoint?,_ corner: CGFloat) {
        let gradientLayer = CAGradientLayer.init()
        gradientLayer.locations = locations
        if startPoint != nil {
            gradientLayer.startPoint = startPoint!
        }
        if endPoint != nil {
            gradientLayer.endPoint = endPoint!
        }
        if corner != 0 {
            gradientLayer.cornerRadius = corner
        }
        gradientLayer.colors = colors
        gradientLayer.frame = self.bounds
        self.layer.insertSublayer(gradientLayer, at: 0)
//        gradientLayer.mask = self.layer
    }
}
